﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

namespace SiteFox.WebParts.Blog
{
    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    
    public class PostsWP : WebPart
    {
        #region PublicProperies
        private int _BlogId = -1;
        [Personalizable(true)]
        public int BlogId
        {
            get { return _BlogId; }
            set { _BlogId = value; }
        }

        private string _ViewPostUrl = "Post.aspx";
        [Personalizable(true)]
        public string ViewPostUrl
        {
            get { return _ViewPostUrl; }
            set { _ViewPostUrl = value; }
        }

        private string _XsltUrl = "~/System/Design/xslt/WebParts/Blog/Posts.xslt";
        [Personalizable(true)]
        public string XsltUrl
        {
            get { return _XsltUrl; }
            set { _XsltUrl = value; }
        }
        #endregion

        public override EditorPartCollection CreateEditorParts()
        {
            ArrayList _editorArray = new ArrayList();
            Blog_Editor _edPart = new Blog_Editor();
            _edPart.ID = this.ID + "_Blog";
            _editorArray.Add(_edPart);
            EditorPartCollection _editorParts = new EditorPartCollection(_editorArray);
            return _editorParts;
        }
        protected override void CreateChildControls()
        {
            Controls.Clear();
            IList<SiteFox.Business.Model.BlogPost> _Posts = null;
            if (HttpContext.Current.Request.QueryString["Category"] != null)
            {
                int _CategoryId = -1;
                try { _CategoryId = Convert.ToInt32(HttpContext.Current.Request.QueryString["Category"].ToString()); }
                catch { }
                if (_CategoryId > -1) 
                {
                    _Posts = SiteFox.Business.Manager.BlogPost.GetByBlogCategory(_CategoryId);
                }
            }
            else if (HttpContext.Current.Request.QueryString["Date"] != null)
            {
                string _DateQuery = HttpContext.Current.Request.QueryString["Date"].ToString();
                DateTime _Date = new DateTime();
                try 
                {
                    _Date = new DateTime(Convert.ToInt32(_DateQuery.Substring(0, 4)), Convert.ToInt32(_DateQuery.Substring(5)), 1);
                }
                catch { }
                _Posts = SiteFox.Business.Manager.BlogPost.GetByArchive(_Date);
            }
            else 
            {
                _Posts = SiteFox.Business.Manager.BlogPost.GetByBlog(_BlogId);
            }
            Xml _Xml = new Xml();
            _Xml.TransformSource = _XsltUrl;
            XmlDocument _XmlDocument = new XmlDocument();
            XmlDeclaration _XmlDeclaration = _XmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlElement _XmlPosts = _XmlDocument.CreateElement("Posts");
            _XmlDocument.InsertBefore(_XmlDeclaration, _XmlDocument.DocumentElement);
            _XmlDocument.AppendChild(_XmlPosts);
            if (_Posts != null) 
            { 
                for (int i = 0; i < _Posts.Count; i++)
                {
                    var _Post = _Posts[i];
                    XmlElement _XmlPost = _XmlDocument.CreateElement("Post");
                    _XmlPost.SetAttribute("Id", _Post.ID.ToString());
                    _XmlPost.SetAttribute("Date", _Post.Created.Value.ToShortDateString());
                    _XmlPost.SetAttribute("Time", _Post.Created.Value.ToShortTimeString());
                    _XmlPost.SetAttribute("Url", _ViewPostUrl + "?Id=" + _Post.ID.ToString());
                    _XmlPost.SetAttribute("CreatedBy", _Post.CreatedBy);
                    _XmlPost.SetAttribute("Comments", _Post.BlogComment.Count.ToString());
                    _XmlPosts.AppendChild(_XmlPost);
                    XmlElement _XmlPostTitle = _XmlDocument.CreateElement("Title");
                    _XmlPostTitle.InnerText = _Post.Title;
                    _XmlPost.AppendChild(_XmlPostTitle);
                    XmlElement _XmlPostDescription = _XmlDocument.CreateElement("Description");
                    _XmlPostDescription.InnerText = _Post.Description;
                    _XmlPost.AppendChild(_XmlPostDescription);
                    XmlElement _XmlPostText = _XmlDocument.CreateElement("Text");
                    _XmlPostText.InnerText = _Post.Text;
                    _XmlPost.AppendChild(_XmlPostText);
                }
            }
            _Xml.DocumentContent = _XmlDocument.InnerXml;
            Controls.Add(_Xml);
        }

        // Create a custom EditorPart to edit the WebPart control.
        [AspNetHostingPermission(SecurityAction.Demand,
          Level = AspNetHostingPermissionLevel.Minimal)]
        private class Blog_Editor : EditorPart
        {
            Label _lab_BlogList;
            DropDownList _ddl_BlogList;
            Label _lab_PostViewUrl;
            TextBox _tb_PostViewUrl;
            Label _lab_XsltUrl;
            TextBox _tb_XsltUrl;
            Literal _lit_Brake;

            public override bool ApplyChanges()
            {
                PostsWP _part = (PostsWP)WebPartToEdit;
                _part._BlogId = Convert.ToInt32(_ddl_BlogList.SelectedValue);
                _part._ViewPostUrl = _tb_PostViewUrl.Text;
                _part._XsltUrl = _tb_XsltUrl.Text;
                return true;
            }
            public override void SyncChanges()
            {
                EnsureChildControls();
                PostsWP _part = (PostsWP)WebPartToEdit;
                if (_part._BlogId > -1)
                {
                    var _Posts = SiteFox.Business.Manager.Blog.GetByID(_part._BlogId);
                    if (_Posts != null)
                    {
                        _ddl_BlogList.SelectedValue = _part._BlogId.ToString();
                    }
                }
                _tb_PostViewUrl.Text = _part._ViewPostUrl;
                _tb_XsltUrl.Text = _part._XsltUrl;
            }
            protected override void CreateChildControls()
            {
                Controls.Clear();
                _lit_Brake = new Literal();
                _lit_Brake.Text = "<br />";
                _lab_BlogList = new Label();
                _lab_BlogList.Text = "Blogs";
                _lab_BlogList.Width = Unit.Percentage(95);
                Controls.Add(_lab_BlogList);
                Controls.Add(_lit_Brake);
                _ddl_BlogList = new DropDownList();
                _ddl_BlogList.Width = Unit.Percentage(95);
                _ddl_BlogList.DataSource = SiteFox.Business.Manager.Blog.GetAll();
                _ddl_BlogList.DataTextField = "Name";
                _ddl_BlogList.DataValueField = "ID";
                _ddl_BlogList.DataBind();
                Controls.Add(_ddl_BlogList);
                Controls.Add(_lit_Brake);
                _lab_PostViewUrl = new Label();
                _lab_PostViewUrl.Text = "Post view url";
                _lab_PostViewUrl.Width = Unit.Percentage(95);
                Controls.Add(_lab_PostViewUrl);
                Controls.Add(_lit_Brake);
                _tb_PostViewUrl = new TextBox();
                _tb_PostViewUrl.Width = Unit.Percentage(95);
                Controls.Add(_tb_PostViewUrl);
                Controls.Add(_lit_Brake);
                _lab_XsltUrl = new Label();
                _lab_XsltUrl.Text = "XSL-T url";
                _lab_XsltUrl.Width = Unit.Percentage(95);
                Controls.Add(_lab_XsltUrl);
                Controls.Add(_lit_Brake);
                _tb_XsltUrl = new TextBox();
                _tb_XsltUrl.Width = Unit.Percentage(95);
                Controls.Add(_tb_XsltUrl);
            }
        }
    }
}
